# Contributor: George Hopkins <george-hopkins@null.net>
# Maintainer: George Hopkins <george-hopkins@null.net>
pkgname=imhex
pkgver=1.19.1
pkgrel=0
_nativefiledialog=28ade5a5cc5d17cea8fe4034572cac8fd54eb53f
_xdgpp=f01f810714443d0f10c333d4d1d9c0383be41375
_libromfs=58757f6cad8fb26cf9a235f2f0bdf9633dfa4a32
_capstone=d5141c04785678535c7792eddc21f146186e639f
_pattern_language=9e984e0bd3382c1f093395916c0a12699bf02a29
_patterns=48dd8f68ca04f56a6111b4c0e936648f271638de
pkgdesc="Hex editor for reverse engineers and programmers"
url="https://github.com/WerWolv/ImHex"
# armhf, armv7: blocked by yara, int128
# ppc64le: textrel in libromfs
# x86: blocked by using int128
arch="all !armhf !armv7 !ppc64le !x86"
license="GPL-2.0-or-later"
options="!check" # No testsuite
makedepends="
	capstone-dev
	cmake
	curl-dev
	file-dev
	fmt-dev
	freetype-dev
	glfw-dev
	glm-dev
	gtk+3.0-dev
	llvm-dev
	llvm-static
	mbedtls-dev
	nlohmann-json
	openssl-dev
	python3-dev
	samurai
	yara-dev
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/WerWolv/ImHex/archive/v$pkgver.tar.gz
	nativefiledialog-$_nativefiledialog.tar.gz::https://github.com/btzy/nativefiledialog-extended/archive/$_nativefiledialog.tar.gz
	libromfs-$_libromfs.tar.gz::https://github.com/WerWolv/libromfs/archive/$_libromfs.tar.gz
	capstone-$_capstone.tar.gz::https://github.com/capstone-engine/capstone/archive/$_capstone.tar.gz
	pattern_language-$_pattern_language.tar.gz::https://github.com/WerWolv/PatternLanguage/archive/$_pattern_language.tar.gz
	imhex-patterns-$_patterns.tar.gz::https://github.com/WerWolv/ImHex-Patterns/archive/$_patterns.tar.gz
	xdg-$_xdgpp.hpp::https://git.sr.ht/~danyspin97/xdgpp/blob/$_xdgpp/xdg.hpp
	"
builddir="$srcdir"/ImHex-$pkgver

prepare() {
	default_prepare

	mv "$srcdir"/nativefiledialog-extended-$_nativefiledialog/* \
		"$builddir"/lib/external/nativefiledialog
	mv "$srcdir"/libromfs-$_libromfs/* \
		"$builddir"/lib/external/libromfs
	cp "$srcdir"/xdg-$_xdgpp.hpp \
		"$builddir"/lib/external/xdgpp/xdg.hpp
	mv "$srcdir"/capstone-$_capstone/* \
		"$builddir"/lib/external/capstone
	mv "$srcdir"/PatternLanguage-$_pattern_language/* \
		"$builddir"/lib/external/pattern_language

	mkdir -p "$builddir"/build/_deps
	mv "$srcdir"/ImHex-Patterns-$_patterns/ \
		"$builddir"/build/_deps/imhex_patterns-src

	# use system deps, new cmake script fails if these are empty
	# even if you set SYSTEM_ vars below..
	rmdir "$builddir"/lib/external/curl
	rmdir "$builddir"/lib/external/fmt

	# cmake script checks if there are at least two files or fails..
	# easier than patching
	touch "$builddir"/lib/external/xdgpp/abc

	sed -i 's|-Werror||g' \
		lib/libimhex/CMakeLists.txt \
		main/CMakeLists.txt \
		plugins/builtin/CMakeLists.txt
}

build() {
	# project cmake doesn't add llvm lib path correctly,
	# this is the easiest fix
	export LDFLAGS="$LDFLAGS -L$(llvm-config --libdir)"
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DUSE_SYSTEM_CURL=ON \
		-DUSE_SYSTEM_NLOHMANN_JSON=ON \
		-DUSE_SYSTEM_FMT=ON \
		-DUSE_SYSTEM_LLVM=ON \
		-DUSE_SYSTEM_YARA=ON \
		-DFETCHCONTENT_FULLY_DISCONNECTED=ON
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
b4d983d1f5ad5697116c419dd58bfc82e65b8c0f6e6e280f8fa57af0cce231b35ea3db69a17ed9d1086c30bec0c4cca4a1a4a6fa4b99344bd25d9dfd13b896ef  imhex-1.19.1.tar.gz
8910a3f80a009de0a14c0efa777e235161f17da9552ae5dd28266dc8eec8459fdc4efc9c067cdc3433201cfc65eafd6d06d28d4360c8d1f01f4118923ac13052  nativefiledialog-28ade5a5cc5d17cea8fe4034572cac8fd54eb53f.tar.gz
7f5e703dc9ab87e19b44728574923ccb8d487ec4ffd69d73d668853372d27779edb3b45c79b041ef96995325343c26ea2c4cfe1864f71b2580709a9ee9531658  libromfs-58757f6cad8fb26cf9a235f2f0bdf9633dfa4a32.tar.gz
de9cf7da9fc3e502165b28b998b472791c8d5366e1a1a1fabd6bb8856a6fae80bf7d1fc4d268572d69c3526161bb0509a598ad4d94207e25a62aff6a5f9e9404  capstone-d5141c04785678535c7792eddc21f146186e639f.tar.gz
e320bddd140fb5e588e3bc94efadce41495a3839d238781c41a59bfa3b1348ad454d55ca07d7151333f75ee016442043e12ed36e38dd68ff82bb9b5f097fc76b  pattern_language-9e984e0bd3382c1f093395916c0a12699bf02a29.tar.gz
acee1cae3fafc181fafb6c72dad8661c22e081e67f46609cff871a663cfdbafc61bea6860cda6b7d48f7ccda5f9edd998f4302fb0c0e42b874a30de91599e221  imhex-patterns-48dd8f68ca04f56a6111b4c0e936648f271638de.tar.gz
0cb56ded30b4eb9d4fc3b4879d9bb5d5c084e67bfe9820264c1ae606f4964e18391cf327fd64b0ca42f89c5c469afa9622b003a1642c584e3da249caefd0373f  xdg-f01f810714443d0f10c333d4d1d9c0383be41375.hpp
"
